#include "bms_Slave.h"
#include "rtwtypes.h"
#include "bms_Slave_DC_ChrgSig.h"
#include "Alarm_Recov_sys.h"
#include <math.h>
#include "bms_Slave_private.h"
#include "plook_u8f_lincka.h"
#include "plook_u8s16_lincka.h"
#include "calPrms.h"

#define bms_Slave_IN_DC_CC             ((uint8_T)1U)
#define bms_Slave_IN_DC_DU             ((uint8_T)2U)
#define bms_Slave_IN_NO_ACTIVE_CHILD_l ((uint8_T)0U)

void bms_Slave_DC_ChrgSig_Init(void)
{
  boolean_T Alarm_flg_hc;
  bms_Slave_DW.UnitDelay1_DSTATE_e = 10.0F;
  bms_Slave_DW.UnitDelay3_DSTATE_p = 10.0F;
  bms_Slave_Alarm_Recov_sys_Init(&bms_Slave_B.Alarm_flg_lh);
  bms_Slave_Alarm_Recov_sys_Init(&bms_Slave_B.Alarm_flg_ha);
  bms_Slave_Alarm_Recov_sys_Init(&Alarm_flg_hc);
}

void bms_Slave_DC_ChrgSig_Reset(void)
{
  boolean_T Alarm_flg_hc;
  bms_Slave_DW.UnitDelay1_DSTATE_e = 10.0F;
  bms_Slave_DW.UnitDelay5_DSTATE_n = 0U;
  bms_Slave_DW.UnitDelay2_DSTATE_c = 0U;
  bms_Slave_DW.UnitDelay1_DSTATE_m = 0;
  bms_Slave_DW.UnitDelay3_DSTATE_m = 0U;
  bms_Slave_DW.UnitDelay6_DSTATE_c = 0U;
  bms_Slave_DW.UnitDelay2_DSTATE_a = false;
  bms_Slave_DW.UnitDelay3_DSTATE_p = 10.0F;
  bms_Slave_DW.is_active_c14_bms_Slave = 0U;
  bms_Slave_DW.is_c14_bms_Slave = bms_Slave_IN_NO_ACTIVE_CHILD_l;
  bms_Slave_DW.Local_flag = 0U;
  bms_Slave_DW.Count = 0U;
  bms_Slave_B.Current_Out = 0U;
  bms_Slave_Alarm_Recov_sys_Reset(&bms_Slave_B.Alarm_flg_lh,
    &bms_Slave_DW.sf_Alarm_Recov_sys1);
  bms_Slave_Alarm_Recov_sys_Reset(&bms_Slave_B.Alarm_flg_ha,
    &bms_Slave_DW.sf_Alarm_Recov_sys3_i1);
  bms_Slave_Alarm_Recov_sys_Reset(&Alarm_flg_hc,
    &bms_Slave_DW.sf_Alarm_Recov_sys3_f);
}

void bms_Slave_DC_ChrgSig_Disable(void)
{
  bms_Slave_DW.DC_ChrgSig_MODE = false;
}

void bms_Slave_DC_ChrgSig(void)
{
  int32_T sigIdx;
  real32_T tmp[15];
  boolean_T rtb_RelationalOperator;
  if (bms_Slave_B.LogicalOperator9) {
    real32_T rtb_Product2;
    real32_T rtb_Switch5;
    uint32_T tmp_0;
    int16_T minV;
    uint8_T rtb_DirectLookupTablenD1;
    uint8_T rtb_Prelookup1;
    uint8_T rtb_Prelookup2;
    boolean_T rtb_LogicalOperator9;
    boolean_T rtb_LogicalOperator_d5;
    boolean_T rtb_RelationalOperator1_km;
    if (!bms_Slave_DW.DC_ChrgSig_MODE) {
      bms_Slave_DC_ChrgSig_Reset();
      bms_Slave_DW.DC_ChrgSig_MODE = true;
    }

    if (bms_Slave_B.Chrg_Sta == 1) {
      tmp_0 = 0U;
    } else {
      tmp_0 = (uint32_T)CellV_num * CellV_limit + Step_Vol;
    }

    bms_Slave_B.DC_ChrgVol = (real32_T)tmp_0 / 1000.0F;
    for (sigIdx = 0; sigIdx < 15; sigIdx++) {
      tmp[sigIdx] = DC_Chrg_CellV[sigIdx];
    }

    rtb_DirectLookupTablenD1 = plook_u8f_lincka(bms_Slave_DW.UnitDelay1_DSTATE,
      tmp, 14U);
    rtb_Prelookup1 = plook_u8s16_lincka(bms_Slave_B.CellT_max, (const int16_T *)
      &DC_Chrg_Temp[0], 8U);
    rtb_Prelookup2 = plook_u8s16_lincka(bms_Slave_B.CellT_min, (const int16_T *)
      &DC_Chrg_Temp[0], 8U);
    if (bms_Slave_DW.UnitDelay_DSTATE_j >= 2) {
      uint8_T tmp_1;
      uint8_T u0;
      if (rtb_Prelookup1 <= 8) {
        tmp_1 = rtb_Prelookup1;
      } else {
        tmp_1 = 8U;
      }

      if (rtb_DirectLookupTablenD1 <= 14) {
        u0 = rtb_DirectLookupTablenD1;
      } else {
        u0 = 14U;
      }

      u0 = DC_Chrg_CC[tmp_1 * 15 + u0];
      if (rtb_Prelookup2 <= 8) {
        tmp_1 = rtb_Prelookup2;
      } else {
        tmp_1 = 8U;
      }

      if (rtb_DirectLookupTablenD1 > 14) {
        rtb_DirectLookupTablenD1 = 14U;
      }

      rtb_DirectLookupTablenD1 = DC_Chrg_CC[tmp_1 * 15 +
        rtb_DirectLookupTablenD1];
      if (u0 <= rtb_DirectLookupTablenD1) {
        rtb_DirectLookupTablenD1 = u0;
      }

      rtb_DirectLookupTablenD1 = (uint8_T)(int32_T)fmodf((real32_T)(int32_T)
        floorf((real32_T)rtb_DirectLookupTablenD1 * 0.75F), 256.0F);
    } else {
      uint8_T tmp_1;
      uint8_T u0;
      if (rtb_Prelookup1 <= 8) {
        tmp_1 = rtb_Prelookup1;
      } else {
        tmp_1 = 8U;
      }

      if (rtb_DirectLookupTablenD1 <= 14) {
        u0 = rtb_DirectLookupTablenD1;
      } else {
        u0 = 14U;
      }

      u0 = DC_Chrg_CC[tmp_1 * 15 + u0];
      if (rtb_Prelookup2 <= 8) {
        tmp_1 = rtb_Prelookup2;
      } else {
        tmp_1 = 8U;
      }

      if (rtb_DirectLookupTablenD1 > 14) {
        rtb_DirectLookupTablenD1 = 14U;
      }

      rtb_DirectLookupTablenD1 = DC_Chrg_CC[tmp_1 * 15 +
        rtb_DirectLookupTablenD1];
      if (u0 <= rtb_DirectLookupTablenD1) {
        rtb_DirectLookupTablenD1 = u0;
      }
    }

    minV = DC_Chrg_Temp[0];
    for (sigIdx = 0; sigIdx < 8; sigIdx++) {
      int16_T u1;
      u1 = DC_Chrg_Temp[sigIdx + 1];
      if (minV > u1) {
        minV = u1;
      }
    }

    if (bms_Slave_DW.is_active_c14_bms_Slave == 0U) {
      bms_Slave_DW.is_active_c14_bms_Slave = 1U;
      bms_Slave_DW.is_c14_bms_Slave = bms_Slave_IN_DC_CC;
      bms_Slave_B.Current_Out = rtb_DirectLookupTablenD1;
      bms_Slave_DW.Count = 0U;
      bms_Slave_DW.Local_flag = 0U;
    } else if (bms_Slave_DW.is_c14_bms_Slave == bms_Slave_IN_DC_CC) {
      bms_Slave_DW.is_c14_bms_Slave = bms_Slave_IN_DC_DU;
    } else {
      if (((bms_Slave_DW.UnitDelay1_DSTATE > CellV_NeverBack) ||
           (bms_Slave_B.CellT_max >= 40) || (bms_Slave_B.CellT_max < 0) ||
           (bms_Slave_B.ChrgMode == 1) || (bms_Slave_DW.Count >= 1)) &&
          (rtb_DirectLookupTablenD1 <= bms_Slave_DW.UnitDelay5_DSTATE_n) &&
          (rtb_DirectLookupTablenD1 < bms_Slave_DW.UnitDelay6_DSTATE_c)) {
        bms_Slave_B.Current_Out = rtb_DirectLookupTablenD1;
        bms_Slave_DW.Local_flag = 1U;
        bms_Slave_DW.Count = 0U;
      } else if (((bms_Slave_DW.UnitDelay1_DSTATE < CellV_NeverBack) &&
                  (rtb_DirectLookupTablenD1 > bms_Slave_DW.UnitDelay6_DSTATE_c) &&
                  (bms_Slave_DW.Local_flag == 0)) || ((bms_Slave_B.CellT_max <
                   35) && (bms_Slave_DW.UnitDelay1_DSTATE <= CellV_ChargBack) &&
                  (bms_Slave_DW.Count >= 20000) && (rtb_DirectLookupTablenD1 >
                   bms_Slave_DW.UnitDelay6_DSTATE_c)) || (((rtb_Prelookup1 >
                    bms_Slave_DW.UnitDelay2_DSTATE_c) || (bms_Slave_B.CellT_max >
        bms_Slave_DW.UnitDelay1_DSTATE_m) || ((bms_Slave_DW.UnitDelay1_DSTATE_m <=
        minV) && (bms_Slave_B.CellT_max >= (int16_T)(minV + 1))) ||
                   (rtb_Prelookup2 > bms_Slave_DW.UnitDelay3_DSTATE_m)) &&
                  (rtb_DirectLookupTablenD1 > bms_Slave_DW.UnitDelay6_DSTATE_c))
                 || ((bms_Slave_B.CellT_max >= 20) && (bms_Slave_B.CellT_max <
                   45) && (bms_Slave_DW.UnitDelay1_DSTATE < CellV_NeverBack) &&
                     (bms_Slave_DW.Count >= 18000) && (rtb_DirectLookupTablenD1 >
        bms_Slave_DW.UnitDelay6_DSTATE_c))) {
        bms_Slave_B.Current_Out = rtb_DirectLookupTablenD1;
        bms_Slave_DW.Count = 0U;
      } else if (bms_Slave_DW.Count < 30000) {
        bms_Slave_DW.Count++;
      } else {
        bms_Slave_DW.Count = 0U;
      }

      if ((bms_Slave_B.Chrg_Sta == 1) || ((bms_Slave_DW.Count >= 18000) &&
           (bms_Slave_B.CellT_max > -10) && (bms_Slave_B.CellT_max < 35) &&
           (bms_Slave_DW.UnitDelay1_DSTATE < CellV_ChargBack))) {
        bms_Slave_DW.Local_flag = 0U;
      }
    }

    rtb_Product2 = (real32_T)bms_Slave_B.Current_Out / 100.0F * 0.01F *
      bms_Slave_B.SOH;
    rtb_Switch5 = rtb_Product2 - bms_Slave_DW.UnitDelay1_DSTATE_e;
    rtb_RelationalOperator = (rtb_Switch5 > 10.0F);
    rtb_RelationalOperator1_km = (rtb_Switch5 < -10.0F);
    rtb_LogicalOperator_d5 = (bms_Slave_DW.UnitDelay2_DSTATE_a ||
      rtb_RelationalOperator || rtb_RelationalOperator1_km);
    rtb_LogicalOperator9 = !rtb_LogicalOperator_d5;
    bms_Slave_Alarm_Recov_sys(rtb_LogicalOperator_d5,
      bms_Slave_DW.UnitDelay2_DSTATE_a || rtb_LogicalOperator9, 1000, 1,
      &bms_Slave_B.Alarm_flg_lh, &bms_Slave_DW.sf_Alarm_Recov_sys1);
    rtb_LogicalOperator_d5 = (bms_Slave_B.ChrgMode == 1);
    bms_Slave_Alarm_Recov_sys(rtb_LogicalOperator_d5, !rtb_LogicalOperator_d5,
      100, 1, &bms_Slave_B.Alarm_flg_ha, &bms_Slave_DW.sf_Alarm_Recov_sys3_i1);
    bms_Slave_DW.UnitDelay2_DSTATE_a = (bms_Slave_B.Alarm_flg_lh ||
      ((rtb_Product2 != bms_Slave_DW.UnitDelay1_DSTATE_e) &&
       (bms_Slave_DW.UnitDelay3_DSTATE_p == bms_Slave_DW.UnitDelay1_DSTATE_e)));
    if (bms_Slave_B.Alarm_flg_ha) {
      sigIdx = 0;
    } else {
      sigIdx = rtb_LogicalOperator_d5;
    }

    if (sigIdx != 0) {
      bms_Slave_DW.UnitDelay1_DSTATE_e = 10.0F;
    } else if ((bms_Slave_B.CellT_max < 45) && bms_Slave_DW.UnitDelay2_DSTATE_a)
    {
      if (rtb_RelationalOperator) {
        bms_Slave_DW.UnitDelay1_DSTATE_e += 10.0F;
      } else if (rtb_RelationalOperator1_km) {
        bms_Slave_DW.UnitDelay1_DSTATE_e -= 10.0F;
      } else {
        bms_Slave_DW.UnitDelay1_DSTATE_e = rtb_Product2;
      }
    } else if (rtb_LogicalOperator9 || (bms_Slave_B.CellT_max >= 45)) {
      bms_Slave_DW.UnitDelay1_DSTATE_e = rtb_Product2;
    }

    bms_Slave_Alarm_Recov_sys(!bms_Slave_B.CellT_Valid, bms_Slave_B.CellT_Valid,
      1, 300, &rtb_RelationalOperator, &bms_Slave_DW.sf_Alarm_Recov_sys3_f);
    bms_Slave_DW.UnitDelay5_DSTATE_n = rtb_DirectLookupTablenD1;
    bms_Slave_DW.UnitDelay2_DSTATE_c = rtb_Prelookup1;
    bms_Slave_DW.UnitDelay1_DSTATE_m = bms_Slave_B.CellT_max;
    bms_Slave_DW.UnitDelay3_DSTATE_m = rtb_Prelookup2;
    bms_Slave_DW.UnitDelay6_DSTATE_c = bms_Slave_B.Current_Out;
    bms_Slave_DW.UnitDelay3_DSTATE_p = rtb_Product2;
  } else if (bms_Slave_DW.DC_ChrgSig_MODE) {
    bms_Slave_DC_ChrgSig_Disable();
  }
}
